package net.scrumtool.controller;

import java.util.List;

import net.scrumtool.model.Collaborator;
import net.scrumtool.model.Playrole;
import net.scrumtool.model.PlayroleId;
import net.scrumtool.model.Product;
import net.scrumtool.model.Role;
import net.scrumtool.technics.DAO;
import net.scrumtool.technics.LabelProviderEditAddCollaborators;
import net.scrumtool.technics.LabelProviderEditRemoveCollaborators;
import net.scrumtool.technics.LabelProviderEditRoleCollaborators;
import net.scrumtool.technics.Utils;
import net.scrumtool.view.EditCollaboratorsProduct;

import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;

public class CeditCollaboratorsProduct {
	EditCollaboratorsProduct view;
	private Collaborator collaboratorActif;
	private Playrole playroleActif;
	private Role roleActif;
	private Role roleEditActif;
	private List<Role> role;

	public CeditCollaboratorsProduct(EditCollaboratorsProduct view) {
		this.view = view;
	}

	public void init(Product product) {
		view.getBtnEditCollaborator().setText("Remove");
		view.getBtnEditCollaborator().setImage(Utils.getImage(Utils.IMG_DEL));
		view.getBtnEditCollaborator().setEnabled(false);
		view.getTableRoleCollaborators().setEnabled(false);
		view.getBtnEditRole().setEnabled(false);
		view.getBtnEditRole().setVisible(false);
		view.getSashForm_1().setWeights(new int[] { 1, 0 });

		view.getTableViewerRemoveCollaborator().setContentProvider(new ArrayContentProvider());
		view.getTableViewerRemoveCollaborator().setLabelProvider(new LabelProviderEditRemoveCollaborators());
		view.getTableViewerRemoveCollaborator().setInput(DAO.getPlayroleByProduct(product));

		view.getTableViewerAddCollaborators().setContentProvider(new ArrayContentProvider());
		view.getTableViewerAddCollaborators().setLabelProvider(new LabelProviderEditAddCollaborators(view));
		view.getTableViewerAddCollaborators().setInput(DAO.getCollaboratorsNotInProduct(product));

		view.getTableViewerRoleCollaborators().setContentProvider(new ArrayContentProvider());
		view.getTableViewerRoleCollaborators().setLabelProvider(new LabelProviderEditRoleCollaborators());
		view.getTableViewerRoleCollaborators().setInput(DAO.getRole());

	}

	public void listener(final Product product) {

		view.getTabFolder().addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent arg0) {
				if (view.getTabFolder().getSelection()[0].getText() == "Collaborator(s) in product") {
					view.getBtnEditCollaborator().setEnabled(false);
					view.getTableRoleCollaborators().setEnabled(false);
					view.getBtnEditCollaborator().setText("Remove");
					view.getBtnEditCollaborator().setImage(Utils.getImage(Utils.IMG_DEL));
				} else {
					view.getSashForm_1().setWeights(new int[] { 1, 0 });
					view.getBtnEditRole().setEnabled(false);
					view.getBtnEditCollaborator().setEnabled(false);
					view.getTableRoleCollaborators().setEnabled(false);
					view.getBtnEditCollaborator().setText("Add");
					view.getBtnEditCollaborator().setImage(Utils.getImage(Utils.IMG_ADD));
				}

			}

			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub

			}
		});

		view.getTableAddCollaborators().addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent arg0) {
				StructuredSelection sel = (StructuredSelection) view.getTableViewerAddCollaborators().getSelection();
				setCollaboratorActif((Collaborator) sel.getFirstElement());

				view.getTableRoleCollaborators().setEnabled(true);

			}

			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub

			}
		});

		view.getTableRoleCollaborators().addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent arg0) {
				StructuredSelection sel = (StructuredSelection) view.getTableViewerRoleCollaborators().getSelection();
				setRoleActif((Role) sel.getFirstElement());

				view.getBtnEditCollaborator().setEnabled(true);

			}

			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub

			}
		});

		view.getTableRemoveCollaborators().addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent arg0) {
				StructuredSelection sel = (StructuredSelection) view.getTableViewerRemoveCollaborator().getSelection();
				setPlayroleActif((Playrole) sel.getFirstElement());

				view.getBtnEditRole().setVisible(true);
				view.getBtnEditCollaborator().setEnabled(true);
				view.getBtnEditRole().setEnabled(true);

			}

			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub

			}
		});

		view.getComboViewer().addSelectionChangedListener(new ISelectionChangedListener() {

			@Override
			public void selectionChanged(SelectionChangedEvent arg0) {
				IStructuredSelection sel = (IStructuredSelection) (view.getComboViewer().getSelection());
				Role role = (Role) (sel.iterator().next());
				setRoleEditActif(role);
				view.getBtnSaveEditRole().setEnabled(true);

			}

		});

		view.getBtnSaveEditRole().addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent arg0) {

				PlayroleId id = new PlayroleId(playroleActif.getCollaborator().getId(), roleEditActif.getId(), product.getId());
				Playrole pl = new Playrole(id, roleEditActif, product, playroleActif.getCollaborator());

				DAO.delPlayrole(playroleActif);
				DAO.addPlayrole(pl);

				view.getSashForm_1().setWeights(new int[] { 1, 0 });
				view.getBtnEditRole().setEnabled(false);
				view.getBtnEditCollaborator().setEnabled(false);
				view.getTableViewerRemoveCollaborator().remove(playroleActif);
				view.getTableViewerRemoveCollaborator().add(pl);

				Utils.MsgBox(view.getShell(), "Role edit successfully", "information");
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub

			}
		});

		view.getBtnEditRole().addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent arg0) {
				view.getBtnSaveEditRole().setEnabled(false);
				view.getSashForm_1().setWeights(new int[] { 277, 138 });

				view.getLblNameCollaboratorEditRole().setText(playroleActif.getCollaborator().getFirstname() + " " + playroleActif.getCollaborator().getLastname());

				List<Role> role = DAO.getRoleWhereNameIsNot(playroleActif.getRole().getName());
				view.getComboViewer().setContentProvider(new ArrayContentProvider());
				view.getComboViewer().setInput(role);
				// view.getComboViewer().setSelection(new
				// StructuredSelection(playroleActif.getRole().getName()));
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub

			}
		});

		view.getBtnEditCollaborator().addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent arg0) {

				if (view.getBtnEditCollaborator().getText() == "Remove") {
					Collaborator col = new Collaborator(playroleActif.getCollaborator().getId(), playroleActif.getCollaborator().getLogin(), playroleActif.getCollaborator().getPassword(), playroleActif.getCollaborator().getFirstname(), playroleActif.getCollaborator().getLastname(), playroleActif.getCollaborator().getEmail(), playroleActif.getCollaborator().getAdministrator(), playroleActif.getCollaborator().getUserstories(), playroleActif.getCollaborator().getParticipates(), playroleActif.getCollaborator().getPlayroles(), playroleActif.getCollaborator().getComments());

					DAO.delPlayrole(playroleActif);

					view.getSashForm_1().setWeights(new int[] { 1, 0 });
					view.getTableViewerRemoveCollaborator().remove(playroleActif);
					view.getTableViewerAddCollaborators().add(col);
					view.getBtnEditCollaborator().setEnabled(false);
					view.getBtnEditRole().setEnabled(false);

					Utils.MsgBox(view.getShell(), "Collaborator delete from product", "information");
				} else {
					// role = DAO.getRole();

					PlayroleId id = new PlayroleId(collaboratorActif.getId(), roleActif.getId(), product.getId());
					Playrole pl = new Playrole(id, roleActif, product, collaboratorActif);

					DAO.addPlayrole(pl);

					view.getTableRoleCollaborators().setEnabled(false);
					view.getBtnEditCollaborator().setEnabled(false);
					view.getTableViewerAddCollaborators().remove(collaboratorActif);
					view.getTableViewerRemoveCollaborator().add(pl);

					Utils.MsgBox(view.getShell(), "Collaborator add successfully", "information");
				}

			}

			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub

			}
		});

	}

	public void setRoleActif(Role roleActif) {
		this.roleActif = roleActif;
	}

	public void setCollaboratorActif(Collaborator collaboratorActif) {
		this.collaboratorActif = collaboratorActif;
	}

	public void setPlayroleActif(Playrole playroleActif) {
		this.playroleActif = playroleActif;
	}

	public void setRoleEditActif(Role roleEditActif) {
		this.roleEditActif = roleEditActif;
	}
}
